#pragma once
//#include "ebmlio.hpp"
#include "mkvstream.hpp"

namespace MkvMux
{

class MkvStreamVideo;
class MkvStreamAudio;

class CStreamIO
{
    CStreamIO(const CStreamIO&);
    CStreamIO& operator=(const CStreamIO&);
    
public:

    CStreamIO();
    ~CStreamIO();
    
    MkvStream::Context m_context;
    
    //Our goals for this muxer are modest.  Instead of a completely
    //general muxer for making MKV files, we only need a single
    //video stream.
    //    
    void SetVideo(MkvStreamVideo*);
    
    //TODO: we would like to eventually support multiple audio streams,
    //e.g. AC-3 5.1 + AC-3 2-channel + director's commentary + etc.
    //For now we only support a single audio stream.
    //
    void SetAudio(MkvStreamAudio*);
    
    //Call this after you've set the video and audio streams, 
    //during the transition from stopped.
    void Init(IStream*);
    
    //Call this during the transition to stopped.
    //It finalizes the file, releases the file stream,
    //and then deallocates the video and audio streams.
    void Final();

    HRESULT WriteVideo(IMediaSample*);
    HRESULT WriteVideoEOS();
    
    HRESULT WriteAudio(IMediaSample*);
    HRESULT WriteAudioEOS();
    
private:

    MkvStreamVideo* m_pVideo;
    MkvStreamAudio* m_pAudio;  //TODO: accept 1+ audio streams

    void WriteEbmlHeader();
    
    void InitSegment();
    void FinalSegment();    

    void InitSeekHead();
    void FinalSeekHead();
    void WriteSeekEntry(ULONG, __int64);

    void InitInfo();
    void FinalInfo();

    void WriteTrack();
    
    __int64 m_seekhead_pos;    
    __int64 m_info_pos;
    __int64 m_track_pos;
    __int64 m_cues_pos;
    __int64 m_duration_pos;    

};


}  //end namespace MkvMux
